#include "non_status_control/non_status_control.hpp"

#ifdef DBG_TAG
#undef DBG_TAG
#endif
#ifdef DBG_LVL
#undef DBG_LVL
#endif

#define DBG_TAG "nonstatus_ctl"
#define DBG_LVL DBG_LOG

extern "C"
{
#include <rtdbg.h>
}

double sonic_height= 0;
/******************************************************************************
 *
 * @brief:    数据储存轮训
 *
 ******************************************************************************/
void record_data_thread_entry(void *pra)
{
    float input_voltage;

    global_data->DATA_SAVE_read_short_cycle_data("IN_V", &input_voltage);
    LOG_D("last input voltage is %f",input_voltage);
    while (1)
    {

        input_voltage = voltage_device->CHIP_ADC_measure();
//        LOG_D("input voltage is %f",input_voltage);

        global_data->DATA_SAVE_write_short_cycle_data("IN_V", input_voltage);
        global_data->DATA_SAVE_record_short_cycle_data();
        rt_thread_mdelay(1000);
    }
}

/******************************************************************************
 *
 * @brief:    光流轮训
 *
 ******************************************************************************/
void flow_thread_entry(void *pra)
{
    flow_device->PMW3901_init();
    while(1)
    {
        flow_device->PWM3901_count_shift_with_height(sonic_height);
        if(flow_device->PWM3901_read_out_shift_and_reduce() == pmw3901_status_ok)
            sim_sig->SIMULATE_SIGNAL_add_a_signal();
        rt_thread_mdelay(2);
    }
}

/******************************************************************************
 *
 * @brief:    超声波轮训
 *
 ******************************************************************************/
void ultra_sonic_thread_entry(void *pra)
{
    chip_uart_message * msg = NULL;
    uint32_t distance;
    int i;
    uint8_t buff1[]={0x01,0x03,0x01,0x02,0x00,0x01,0x24,0x36};//读取温度
    uint8_t buff2[]={0x01,0x03,0x01,0x00,0x00,0x01,0x85,0xF6};//读取处理距离值
    while(1)
    {
        ultra_sonic_en->CHIP_OUTPUT_PIN_write(chip_pin_level_HIGH);
        ultra_sonic_uart->CHIP_UART_send(8, buff2);
        ultra_sonic_en->CHIP_OUTPUT_PIN_write(chip_pin_level_LOW);

        msg = ultra_sonic_uart->CHIP_UART_read_receive(1000);
        if(msg)
        {
//            LOG_D("recevied!");
//            for(i = 0;i < msg->now_end_pos;i++)
//            {
//                LOG_D("<%d>:<0x%.2X>",i,msg->message[i]);
//            }
            distance = 0;
            distance |= (msg->message[3]<<8);
            distance |= (msg->message[4]);
//            LOG_D("distance %u mm ",distance);
            sonic_height = (double)distance;
            delete msg;
            msg = NULL;
        }
        else
        {
            LOG_D("no recevied!");
        }
//        flow_device->PMW3901_burst_read_motion(&motion);
        rt_thread_mdelay(100);
    }
}

/******************************************************************************
 *
 * @brief:    开启所有无状态控制
 *
 ******************************************************************************/

void non_status_control_thread_start()
{
    rt_thread_t tid;

    record_tid = rt_thread_create("record", record_data_thread_entry, RT_NULL, 2048, 16, 20);
    if (record_tid)
        rt_thread_startup(record_tid);
    else
        LOG_E("record thread not start!");

    tid = rt_thread_create("flow", flow_thread_entry, RT_NULL, 2048, 15, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("flow thread not start!");

    tid = rt_thread_create("sonic", ultra_sonic_thread_entry, RT_NULL, 2048, 15, 20);
    if (tid)
        rt_thread_startup(tid);
    else
        LOG_E("sonic thread not start!");


}

